package org.lex.expression;

import java.util.HashMap;
import java.util.Map;

public class Token {
	public static final Token END = new Token(TokenType.END, "");
	public static final Token UNKOWN = new Token(TokenType.UNKOWN, "");
	private static final Map<Character, Token> cache = new HashMap<Character, Token>();

	private TokenType tokenType = TokenType.UNKOWN;
	private String image = "";

	public static Token parseOperatorToken(char operator) {
		Token token = cache.get(operator);
		if (null == token) {
			token = new Token(operator);
			cache.put(operator, token);
		}
		return token;
	}

	private Token(char operator) {
		this(TokenType.parseOperatorType(operator), String.valueOf(operator));
	}

	public Token(TokenType type, String image) {
		this.tokenType = type;
		this.image = image;
	}

	public String getImage() {
		return image;
	}

	public TokenType getTokenType() {
		return tokenType;
	}

	public boolean isOperator() {
		switch (this.tokenType) {
		case ADD:
		case MINUS:
		case MULTIPLY:
		case DIVIDE:
		case MOD:
		case POWER:
		case LEFT_P:
		case RIGHT_P:
			return true;
		default:
			return false;
		}
	}

	public boolean isOperand() {
		return this.tokenType == TokenType.NUMERIC;
	}

	public String toString() {
		return this.image + "\t[" + this.tokenType.name() + "]";
	}
}
